package com.lee.demo1.controller;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Counter extends Object {
	
	private static Logger logger = LoggerFactory.getLogger(ArticleController.class);
    private String currentRecord = null;
    private BufferedReader file;
    private String path;

    public Counter() {}

    public String ReadFile(String filePath) throws FileNotFoundException {
        path = filePath;
        
        file = new BufferedReader(new FileReader(path));
        String returnStr = null;
        try {
            currentRecord = file.readLine();
        } catch (IOException e) {
        	logger.error("读取数据错误.");
        }
        if (currentRecord == null)
            returnStr = "没有任何记录";
        else {
            returnStr = currentRecord;
        }
        return returnStr;
    }
    public void WriteFile(String filePath, String counter)
            throws FileNotFoundException {
        path = filePath;
        
        int Writestr = Integer.parseInt(counter) + 1;
        try {
            PrintWriter pw = new PrintWriter(new FileOutputStream(filePath));
            pw.println(Writestr);
            pw.close();
        } catch (IOException e) {
        	logger.error("写入文件错误" + e.getMessage());
        }
    }
}